package com.xpgk.xpsoft.code;

import java.util.List;

import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Record;
import com.xpgk.common.kit.CacheKitEx;
import com.xpgk.common.kit.PubKit;
import com.xpgk.xpsoft.common.XPSoftService;
import com.xpgk.xpsoft.constants.XPSoftConstant;

public class CodeTypeService extends XPSoftService {
	static public synchronized String saveType(Record type) {
		Integer value = type.getInt("systemID");
		if (value == null)
			type.set("systemID", XPSoftConstant.systemID);

		type.remove("id");
		if (ExistCodeType(type.getInt("systemID"), type.getStr("type")) != null) {
			return "had exist type";
		}
		;
		Db.save("frm_code_type", type);
		CacheKitEx.remove(XPSoftConstant.cacheName, XPSoftConstant.cacheKey_CodeTypeList);
		return "ok";
	}

	// 添加用户
	static public Integer ExistCodeType(Integer systemID, String type) {
		return Db.queryInt("select id from frm_code_type where systemID=? and type=?", systemID, type);
	}

	static public String updateType(Record type) {
		if (type == null || type.get("id") == null)
			return "错误信息。";
		Integer id = ExistCodeType(type.getInt("systemID"), type.getStr("type"));
		if (id != null && id != PubKit.toInt(type.get("id"))) {
			return "had exist type";
		}
		;
		if (Db.update("frm_code_type", type)) {
			CacheKitEx.remove(XPSoftConstant.cacheName, XPSoftConstant.cacheKey_CodeTypeList);
			return "ok";
		}
		return "错误信息。";
	}

	static public List<Record> getTypeList() {
		List<Record> typeList = CacheKitEx.get(XPSoftConstant.cacheName, XPSoftConstant.cacheKey_CodeTypeList);
		if (typeList == null) {
			typeList = Db.find("select type,note from frm_code_type order by note");
			CacheKitEx.put(XPSoftConstant.cacheName, XPSoftConstant.cacheKey_CodeTypeList, typeList);
		}
		return typeList;
	}
}
